package com.sgzn.sgpay.service.tcp.model.terminal.request;

import com.sgzn.sgpay.service.tcp.SgTcpProtocolHead;
import com.sgzn.sgpay.service.tcp.util.SgTcpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * 查询信号应答 请求类
 * @author huangsenguang
 * @version Id: TcpQuerySignalAnswerRequest.java, v 0.1 2018/7/23 14:44 huangsenguang Exp $$
 */
@Data
@Slf4j
public class TcpQuerySignalAnswerRequest extends TcpCommonRequest {
    /**
     * 长度: 16进制字符串
     */
    private String length="05";

    /**
     * 索引: 16进制字符串
     */
    private String index="01";

    /**
     * 命令: 16进制字符串
     */
    private String cmd="19";

    /**
     * data[0]:信号值 0-31种等级
     */
    private String signalValue;

    /**
     * data[1]:链接服务器状态 0：在线  1：掉线
     */
    private String linkServerState;



    @Override
    public String toString(){
        //根据协议规范重载toString方法
        return beginCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                serialCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                deviceId + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                commandReq + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                head + length + index + cmd + getData() +
                getAllCheck(length + index + cmd + getData()) + end + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                checkSumCode + SgTcpProtocolHead.TCP_SEND_SEPERATOR +
                endCode;
    }

    private String getData() {
        return SgTcpUtil.cutOrSupplement(signalValue,2) + SgTcpUtil.cutOrSupplement(linkServerState,2);
    }

    /**
     * 获取校验码
     * @param needToCheck
     * @return
     */
    private String getAllCheck(String needToCheck) {
        this.setCheck(SgTcpUtil.getToCheck(needToCheck));
        return this.check;
    }


    public static void main(String[] args){
        TcpQuerySignalAnswerRequest tcpRequest = new TcpQuerySignalAnswerRequest();
        tcpRequest.setDeviceId("10001");
        tcpRequest.setSignalValue("31");
        tcpRequest.setLinkServerState("00");
        log.info(tcpRequest.toString());
        System.out.println("tcpRequest.toString() = " + tcpRequest.toString());
    }
}

